package loadScore;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import elementos.Ranking;

public class Score implements ScoreManager{
    
    String name;
    Ranking rank;

    public Score(int level) {
        name="/Users/pedro/ranking"+level+".txt";
        rank=loadRanking();
        if (rank==null)rank=new Ranking();
    }
    
    

    @Override
    public void saveScore(String id, Float score,Integer level) {
        rank.addScore(id,score);
        FileOutputStream file;
        ObjectOutputStream out;
        try {
            file = new FileOutputStream(name);
            out = new ObjectOutputStream(file);
            out.writeObject(rank);
            out.close();
            file.close();                 
            
        } catch (Exception ex) {
            Logger.getLogger(Score.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public Ranking loadRanking() {
        FileInputStream file;
        ObjectInputStream in;
        try {
            file = new FileInputStream(name);
            in = new ObjectInputStream(file);
            Ranking rank = (Ranking)in.readObject();
            in.close();
            file.close();
            return rank;
        } catch (Exception ex) {
            Logger.getLogger(Score.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }
    
}
